package edu.hawaii.examschd.modules;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.StringTokenizer;
import edu.hawaii.examschd.myClass;
import edu.hawaii.examschd.modules.CouchDB.CouchDB;

/**
 * Creates a file in ICS format
 * 
 * @author johnmacmillan
 * 
 */
public class Cal implements Module {

  boolean DEBUG = false;
  boolean VERBOSE = false;
  Calendar dStart, dEnd;
  DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
  File file;
  FileOutputStream out;
  OutputStreamWriter writer;

  public Cal() {

  }

  /**
   * Create the ICS file from a string.
   * 
   * @param input in the form of "start,end, summary, loc, desc"
   * @return s ICS string to be written to file;
   */
  public String createICS(String input) {
    StringTokenizer st = new StringTokenizer(input, ",");
    String s;
    s = "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nBEGIN:VEVENT\r\n";
    s = s + "DTSTART:" + st.nextToken() + "\r\n";
    s = s + "DTEND:" + st.nextToken() + "\r\n";
    s = s + "SUMMARY:" + st.nextToken() + "\r\n";
    s = s + "LOCATION:" + st.nextToken() + "\r\n";
    s = s + "DESCRIPTION:" + st.nextToken() + "\r\n";
    s = s + "PRIORITY:3\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";
    return s;
  }

  /**
   * Creates a csv string from a cal object of "start,end, summary, loc, desc" yyyyMMdd'T'HHmmss
   * 
   * @param cal
   * @return
   */
  public String createICS(myClass classObj) {
    String output = "";
    String start = dateFormat.format(classObj.get_exam_cal().getTime());
    String end = start.substring(9, 11);
    int i = Integer.parseInt(end) + 2;
    end = start.substring(0, 9) + i + start.substring(11);
    String summary = classObj.get_dept() + " " + classObj.get_num() + " Final";
    output = start + "," + end + "," + summary + ", , ";
    if (DEBUG) {
      System.out.println("s: " + start + "\r\ne: " + end);
    }
    output = createICS(output);
    return output;
  }

  /**
   * Changes a string from "year,month,day,hour,minute" to the format needed for ICS.
   */
  private String process_input(String input) {
    return "";
  }

  /**
   * 
   * @param classObj
   * @param type
   * @return
   */
  public File makeFile(myClass classObj) {
    String dept = classObj.get_dept().toLowerCase();
    FileWriter fw;
    int num = classObj.get_num();
    String prefix = dept + num;
    String content;
    File temp = null;

    try {
      temp = File.createTempFile(prefix, ".ics");
    }
    catch (IOException ioe) {
      print("Temp file creation failed!");
    }
    if (temp.exists()) {
      try {
        fw = new FileWriter(temp);
        content = createICS(classObj) + "\r\n";
        fw.close();
      }
      catch (IOException ioe) {
        print("File writing failed");
      }
    }
    return temp;

  }

  /**
   * Make calendar from crn input
   */
  public File makeCal(int crn) {
    CouchDB couchdb = new CouchDB();
    myClass classObj = couchdb.get_all_by_crn(crn);
    File file = makeFile(classObj);
    return file;
  }

  /**
   * load module
   */
  @Override
  public int load() {
    // TODO Auto-generated method stub
    return 0;
  }

  /**
   * Sync VERBOSE and DEBUG vars with main application;
   */
  @Override
  public int syncVars(boolean d, boolean v) {
    if (d) {
      DEBUG = d;
      VERBOSE = v;
    }
    else if (v) {
      VERBOSE = v;
    }
    return 0;
  }

  @Override
  public int unload() {
    // TODO Auto-generated method stub
    return 0;
  }

  @Override
  public String getName() {
    return "Calendar";
  }

  private void print(String s) {
    System.out.println(s);
  }
}